import { useEffect, useRef } from 'react';
import { EZUIKitPlayer } from 'ezuikit-js';

const index = ({
  url = 'ezopen://open.ys7.com/BD3957004/1.live',
  accessToken = 'ra.9q6lamgj0xej60na0d8o3uk55cqqv80w-3d4yk333vm-09dx7bz-tog0swvfq',
}) => {
  const videoRef = useRef(null);
  const videoId = `video-${new Date().getTime()}`;
  useEffect(() => {
    if (videoRef.current) {
      const player = new EZUIKitPlayer({
        id: videoId,
        accessToken: accessToken,
        url: url,
        handleError: (err) => {
          if (err.type === 'handleRunTimeInfoError' && err.data.nErrorCode === 5) {
            console.log('加密设备密码错误');
          }
        },
      });

      player.play(); // 播放视频

      // 清理
      return () => {
        player.stop(); // 停止播放器
      };
    }
  }, [url, accessToken]);

  return (
    <div>
      <video ref={videoRef} id={videoId} className="w-full h-full block" />
    </div>
  );
};

export default index;
